package com.sgb.kjwl.view.ui.activity.manage

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityAgentApplyBinding
import com.sgb.link.entity.PickFilterEntity
import com.sgb.kjwl.view.ui.fragment.manage.AgentCustomerCheckFragment
import com.sgb.kjwl.view.ui.fragment.manage.AgentStaffCheckFragment
import com.sgb.kjwl.view.ui.fragment.manage.OtherInquiryFragment

/**
 * 代办申请
 */
class AgentApplyActivity : AppActivity() {
    private lateinit var mBinding : ActivityAgentApplyBinding
    private var mTitle : ArrayList<PickFilterEntity<Any>> ?= null
    private lateinit var mAdapter : AgentApplyPageAdapter
    private var mName : String = ""

    companion object{
        const val KEY_DATAS = "key_datas"
        const val KEY_NAME = "key_name"
        fun launch(context: Context, titles : ArrayList<PickFilterEntity<Any>>){
            this.launch(context, titles,"")
        }

        fun launch(context: Context, titles : ArrayList<PickFilterEntity<Any>>, name : String = ""){
            val intent = Intent(context, AgentApplyActivity::class.java)
            intent.putExtra(KEY_DATAS, titles)
            intent.putExtra(KEY_NAME, name)
            context.startActivity(intent)
        }
    }


    override fun initView() {
        super.initView()
        if (intent != null){
            mTitle = intent.getSerializableExtra(KEY_DATAS) as ArrayList<PickFilterEntity<Any>>?
            mName = intent.getStringExtra(KEY_NAME)
        }
        if (mTitle == null || mTitle!!.isEmpty()){
            finish()
            return
        }
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_agent_apply)
        mBinding.include.ivBack.setOnClickListener { finish() }
        mBinding.include.titleView.text = if (TextUtils.isEmpty(mName)) "待办申请" else mName
        mAdapter = AgentApplyPageAdapter(supportFragmentManager, mTitle!!)
        mBinding.viewPage.adapter = mAdapter
        mBinding.viewPage.offscreenPageLimit = mTitle!!.size
        mBinding.tablayout.setupWithViewPager(mBinding.viewPage)
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        mBinding.viewPage.currentItem = 0
        mBinding.tablayout.visibility = if (mTitle!!.size == 1) View.GONE else View.VISIBLE
    }


    class AgentApplyPageAdapter(fm : FragmentManager, titles : ArrayList<PickFilterEntity<Any>>) : FragmentPagerAdapter(fm) {
        private var mTitles : ArrayList<PickFilterEntity<Any>> ?= null
        init {
            this.mTitles = titles
        }
        override fun getCount(): Int {
            if (mTitles != null){
                return mTitles!!.size
            }
            return 0
        }

        override fun getItem(position: Int): Fragment {
            var no = mTitles!![position].no
            when(no) {
                "9" -> { //员工审核
                    return AgentStaffCheckFragment.newInstance()
                }
                "10" -> { //会员审核
                    return  AgentCustomerCheckFragment.newInstance()
                }
            }
            return OtherInquiryFragment.newInstance()
        }

        override fun getPageTitle(position: Int): CharSequence {
            if (mTitles != null){
                return mTitles!![position].name
            }
            return ""
        }
    }
}